package kotlinTest

import org.joda.time.DateTime
import org.joda.time.Days
import org.joda.time.format.DateTimeFormat
import org.junit.Test
import java.lang.RuntimeException

/**
 * Created by futao on 2017/9/25.
 */

class User{
    var name:String=""
    var age:Int=0
}
class Case01 {
    fun DateTime.dayFirstTime():DateTime{
        return DateTime(this.year,this.monthOfYear,this.dayOfMonth,0,0)
    }
    @Test
    fun case001(){
//        for (item in DateTime.now().minusDays(10).dayFirstTime() ..  DateTime.now().plus(20).dayFirstTime()){
//        }
        val td=DateTime.now().dayFirstTime()
        println(Days.daysBetween(DateTime.now().dayFirstTime(), DateTime.now().plusDays(1).dayFirstTime()).days)
        val list= ArrayList<DateTime>()
        list.add(DateTime.now().dayFirstTime())
        val time=DateTime.now().dayFirstTime()
        for(d in 1..(Days.daysBetween(DateTime.now().dayFirstTime(), DateTime.now().plusDays(1).dayFirstTime()).days).toInt())
        {
            println(time)
            println(time.plusDays(1))
            list.add(time.plusDays(1))
            println(list)
        }
    }
//    fun case002(){
//        println(Days.daysBetween("2017-5-20","2017-05-22").days)
//    }
}

val DateTimeFormatterDay = DateTimeFormat.forPattern("yyyy-MM-dd")
val DateTimeFormatterMinute = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm")
val DateTimeFormatterSecond = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
val DateTimeFormatterMillis = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS")

fun String?.parseDate():DateTime?{
    if(null == this) return null
    try{
        if(this.length == 10) return DateTime.parse(this, DateTimeFormatterDay)
        else if(this.length == 16) return DateTime.parse(this, DateTimeFormatterMinute)
        else if(this.length == 19) return DateTime.parse(this, DateTimeFormatterSecond)
        else if(this.length == 23) return DateTime.parse(this, DateTimeFormatterMillis)
        return null
    }catch (re: RuntimeException){
        return null
    }
}